#+TITLE: Emacs configuration

Inspired by [[https://github.com/hrs/dotfiles/tree/master/emacs.d][hrs]] and [[https://github.com/pierre-lecocq/emacs.d/tree/literal][pierre-lecocq]] org configs

* General
** Package management

   Setup repositories for package and auto download use-package if it's missing. 
   #+BEGIN_SRC emacs-lisp
     (require 'package)

     (setq package-archives
           '(("melpa"        . "http://melpa.org/packages/")
             ("gnu"          . "http://elpa.gnu.org/packages/")
             ;; ("marmalade"    . "http://marmalade-repo.org/packages/")
             ("org"          . "http://orgmode.org/elpa/")))

     (package-initialize)

     (when (not package-archive-contents)
       (package-refresh-contents))

     (setq package-enable-at-startup nil)

     (unless (package-installed-p 'use-package)
       (package-install 'use-package))
   #+END_SRC

   Let use-package handle all the packages I use
   #+BEGIN_SRC emacs-lisp
     (eval-when-compile
       (require 'use-package))
     (require 'diminish)                ;; if you use :diminish
     (require 'bind-key)                ;; if you use any :bind variant
   #+END_SRC
** Customizations
   Set emacs to save the customizations in a separate file to avoid
   having them in the git repo.
   #+BEGIN_SRC emacs-lisp
     (setq custom-file "~/.emacs.d/.custom.el")
     (when (file-exists-p "~/.emacs.d/.custom.el") (load custom-file))
   #+END_SRC
** Custom plugins
*** Dotemacs
    Export a variable if I'm at work or not.
    #+BEGIN_SRC emacs-lisp
      (use-package dotemacs
       	:load-path "~/.emacs.d/plugins")
    #+END_SRC
** UI
*** Maximize work area
   Disable the menu, tool and scroll bar since they just take up
   space.
   #+BEGIN_SRC emacs-lisp
     (menu-bar-mode 0)
     (tool-bar-mode 0)
     (scroll-bar-mode 0)
   #+END_SRC
*** Splash screen
    Dismiss the startup screen
    #+BEGIN_SRC emacs-lisp
      (setq inhibit-splash-screen t)
    #+END_SRC
*** Font
    Set font and font size
    #+BEGIN_SRC emacs-lisp
      (set-face-attribute 'default nil :height 92)
      (set-face-attribute 'default nil :font "Hack")
    #+END_SRC
*** Column number
   Show column number in the info bar
   #+BEGIN_SRC emacs-lisp
     (column-number-mode 1)
   #+END_SRC
** Behaviour
   Highlight matching brackets
   #+BEGIN_SRC emacs-lisp
     (show-paren-mode 1)
   #+END_SRC
   
   Disable electric-indent-mode since I"m using C-j for that.
   #+BEGIN_SRC emacs-lisp
     (setq electric-indent-mode nil)
   #+END_SRC

   To avoid loading outdated byte code files, prefer loading newer .el
   files.
   #+BEGIN_SRC emacs-lisp
     (setq load-prefer-newer t)
   #+END_SRC
** Backup/autosave
   One annoying thing with emacs is it that it leaves backup files all
   over the place.  Fixing that by dumping all backup/autosave files
   in to one directory.
   #+BEGIN_SRC emacs-lisp
     (defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
     (defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
     (setq backup-directory-alist (list (cons ".*" backup-dir)))
     (setq auto-save-list-file-prefix autosave-dir)
     (setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))
   #+END_SRC

** Theme
   Point emacs to where it can find the sunburst theme. Depending on
   if emacs is started in a shell or not load the correct theme.
   #+BEGIN_SRC emacs-lisp
     (add-to-list 'custom-theme-load-path "~/.emacs.d/themes/sunburst-theme")
     (if window-system (load-theme 'sunburst t) (load-theme 'sunburst_term t))
   #+END_SRC
** Calendar
   Week starts on Monday.
   #+BEGIN_SRC emacs-lisp
     (setq calendar-week-start-day 1)
   #+END_SRC
* Packages
** Auto complete
   Init auto-complete to use the default settings. Except auto
   complete words when pressing tab but disable it for enter.

   Disable autocomplete when typing // comments in c++.

   For the c-family (c++, c and c#) 
   ac-omni-completion-sources is made buffer local so you need to add
   it to a mode hook to activate on whatever buffer you want to use it
   with.  This example uses C mode (as you probably surmised).
   
   auto-complete.el expects ac-omni-completion-sources to be a list of
   cons cells where each cell's car is a regex that describes the
   syntactical bits you want AutoComplete to be aware of. The cdr of
   each cell is the source that will supply the completion data.  The
   following tells autocomplete to begin completion when you type in a
   . or a ->

   Enable autocomplete for arduino and gmake's makefile since they're
   not on by default.
   
   Note don't put the keybindings under :bind then this will not be
   loaded.
   #+BEGIN_SRC emacs-lisp
     (use-package auto-complete-config
       :disabled t
       :config
       (ac-config-default)
       (define-key ac-completing-map "\t" 'ac-expand)
       (define-key ac-completing-map "\r" nil)

       ;; Ignore c++ comments
       (add-hook 'c++-mode-hook
                 (lambda ()
                   (make-local-variable 'ac-ignores)
                   (add-to-list 'ac-ignores "//")))
       ;; C-common: Start auto complete on . or ->
       (add-hook 'c-mode-common-hook 
                 '(lambda ()
                    (add-to-list 'ac-omni-completion-sources
                                 (cons "\\." '(ac-source-semantic)))
                    (add-to-list 'ac-omni-completion-sources
                                 (cons "->" '(ac-source-semantic)))
                    ))
       ;; Enable ac for gmake and arduino
       (add-to-list 'ac-modes 'makefile-gmake-mode)
       (add-to-list 'ac-modes 'arduino-mode))
   #+END_SRC
** Arduino
   Function for setting up a arduino template sketch
   #+BEGIN_SRC emacs-lisp
     (defun init-arduino ()
     "Template arduino sketch"
     (interactive)
     (insert "void setup() {
       // put your setup code here, to run once:

     }

     void loop() {
       // put your main code here, to run repeatedly:

     }")
     )   
   #+END_SRC
** Company
   
   #+BEGIN_SRC emacs-lisp
     (use-package company
       :bind ("C-<tab>" . company-complete)
       :init
         (global-company-mode 1)
       :config
       (setq company-dabbrev-downcase 0)
       (setq company-idle-delay 0)
       ;; (defun tab-indent-or-complete ()
       ;;   (interactive)
       ;;   (if (minibufferp)
       ;;    (minibuffer-complete)
       ;;     (if (or (not yas-minor-mode)
       ;;          (null (do-yas-expand)))
       ;;      (if (check-expansion)
       ;;          (company-complete-common)
       ;;        (indent-for-tab-command)))))
       ;; (global-set-key [backtab] 'tab-indent-or-complete)
       (add-to-list 'company-backends 'company-c-headers))
   #+END_SRC
** Gnuplot
   Enable gnuplot to be able to plot tables in org mode.

   Bind the F9 key to open a buffer into gnuplot mode

   Set that all files ending in .gp will use the gnuplot-mode
   #+BEGIN_SRC emacs-lisp
     (use-package gnuplot
       :bind ([(f9)] . gnuplot-make-buffer)
       :config
       (autoload 'gnuplot-mode "gnuplot" "gnuplot major mode" t)
       (autoload 'gnuplot-make-buffer "gnuplot" "open a buffer in gnuplot mode" t)
       ;; Set files with ext .gp to use gnuplot
       (setq auto-mode-alist (append '(("\\.gp$" . gnuplot-mode)) auto-mode-alist)))
   #+END_SRC
** CC mode
   Specific for C, C++ and other in the c family
   - Set indentation to be two spaces.
   - Set the default mode for .h files to be c++-mode
   - Make it easier to work with camelCase words by enabling subword-mode.
   - Add that it will also search src and include directories when
     switching between header and source files.

   #+BEGIN_SRC emacs-lisp
     (use-package cc-mode
     :mode ("\\.h\\'" . c++-mode)
     :config
     (add-hook 'c-mode-common-hook
         (lambda ()
           (setq indent-tabs-mode nil)
           (setq c-basic-offset 2)
           (subword-mode 1))) ;; enable camelCase
     (setq ff-search-directories '("." "../src" "../include")))

   #+END_SRC
** Flycheck
   Enable flycheck globably.

   Disable clang check, gcc check works better.
   #+BEGIN_SRC emacs-lisp
     (use-package flycheck
       :config
       (add-hook 'after-init-hook #'global-flycheck-mode)
       (setq-default flycheck-disabled-checkers
		     (append flycheck-disabled-checkers '(c/c++-clang)))
       (when dotemacs-is-work 
	 (setq-default flycheck-c/c++-gcc-executable
		       "/tools/package/gcc/6.2.0/bin/g++")))
   #+END_SRC
   Tell emacs it's safe to change these in a .dir-locals.el file.
   #+BEGIN_SRC emacs-lisp
     (put 'flycheck-c/c++-gcc-executable 'safe-local-variable #'stringp) 
     (put 'flycheck-gcc-args 'safe-local-variable #'listp) 
   #+END_SRC
** Golang
   Setting up go to use 2 spaces as indentation and enable
   autocomplete for go.
   #+BEGIN_SRC emacs-lisp
     (use-package go-mode
     :config
     (add-hook 'go-mode-hook 
       (lambda ()
         (setq-default) 
         (setq tab-width 2) 
         (setq standard-indent 2) 
         (setq indent-tabs-mode nil)))
     (use-package go-autocomplete))
   #+END_SRC
** Haskell
   Settings for programming haskell in emacs
   #+BEGIN_SRC emacs-lisp
     (use-package haskell-mode
       :config
       (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
       (add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
       (autoload 'ghc-init "ghc" nil t))
   #+END_SRC
** Ledger
   Settings for ledger.
   Set the default mode for .dat files to ledger.

   Clean the buffer with C-c c.
   #+BEGIN_SRC emacs-lisp
     (use-package ledger-mode
       :bind (:map ledger-mode-map ("C-c c" . ledger-mode-clean-buffer))
       :mode "\\.dat\\'"
       :config
       (setq ledger-clear-whole-transactions 1))
   #+END_SRC
** Lisp
   Color haxvalues with their respective color.
   #+BEGIN_SRC emacs-lisp
     (use-package lisp-mode
       :config
       (defvar hexcolour-keywords
         '(("#[[:xdigit:]]\\{6\\}"
            (0 (put-text-property (match-beginning 0)
                                  (match-end 0)
                                  'face (list :background 
                                              (match-string-no-properties 0)))))))
       (defun hexcolour-add-to-font-lock ()
         (font-lock-add-keywords nil hexcolour-keywords))
       (add-hook 'lisp-mode-hook 'hexcolour-add-to-font-lock))
   #+END_SRC
** GLSL
   Set files associated with glsl to use glsl mode
   #+BEGIN_SRC emacs-lisp
         (use-package glsl-mode
           :mode (("\\.vert\\'" . glsl-mode)
                  ("\\.frag\\'" . glsl-mode)
                  ("\\.geom\\'" . glsl-mode)
                  ("\\.prog\\'" . glsl-mode)
                  ("\\.glsl\\'" . glsl-mode)))
   #+END_SRC
** Python
   Package name is python but the mode is python-mode
   Set indentation to 2 white spaces.

   Set the default for pb2 files (=PROJECT=) to use python.
   #+BEGIN_SRC emacs-lisp
     (use-package python
       :mode (("\\.py\\'" . python-mode) 
              ("^PROJECT$" . python-mode))
       :interpreter ("python" . python-mode)
       :config
       (setq python-mode-hook
             (function (lambda ()
                         (setq indent-tabs-mode nil)
                         (setq python-indent-offset 2)))))
   #+END_SRC
** RTags (Disabled)
   Key bindings for RTags (they conflicts with GTags)
   #+BEGIN_SRC emacs-lisp
     (use-package rtags
     :disabled t
     :bind (("M-." . rtags-find-symbol-at-point)
            ("M-," . rtags-find-references-at-point)
            ("M-[" . rtags-location-stack-back)
            ("M-]" . rtags-location-stack-forward))
     :config
     (use-package rtags-ac))
   #+END_SRC
** Sh
   Indent using 2 spaces for shell scripts.
   #+BEGIN_SRC emacs-lisp
     (use-package sh-script
       :config
       (add-hook 'sh-mode-hook
             (lambda ()
               (setq indent-tabs-mode nil)
               (setq c-basic-offset 2))))
   #+END_SRC
** Shell
   Enable color in shell and define the color theme. Also disable
   yasnippet in shell mode since that's messing with the shell.
   
   Custom function to clear the shell in emacs. Bound to f8
   #+BEGIN_SRC emacs-lisp
     (use-package shell
       :bind ("<f8>" . clear-shell)
       :config
       (add-hook 'shell-mode-hook 
                 (lambda ()
                   ;; Enable color in shell
                   (ansi-color-for-comint-mode-on)
                   ;; Change Color theme in shell
                   (setq ansi-color-names-vector
                         ["#4d4d4d"
                          "#D81860"
                          "#60FF60"
                          "#f9fd75"
                          "#4695c8"
                          "#a78edb"
                          "#43afce"
                          "#f3ebe2"])
                   (setq ansi-color-map (ansi-color-make-color-map))
                   ;; Disable yas minor mode
                   (yas-minor-mode -1)
                   ;; Add go and goc to the dirtrack, Need tweak the regexp 
                   ;; (setq shell-cd-regexp "\\(cd\\|goc\\|go\\)")
                   ))
       (defun clear-shell ()
         "Clear the shell buffer"
         (interactive)
         (let ((comint-buffer-maximum-size 0))
           (comint-truncate-buffer))))
   #+END_SRC
** Tramp
   Set the ssh to be the default method for tramp
   #+BEGIN_SRC emacs-lisp
     (use-package tramp
       :config
       (setq tramp-default-method "ssh"))
   #+END_SRC
** Yasnippet
   Enable yasnippet
   #+BEGIN_SRC emacs-lisp
     (use-package yasnippet 
       :config
       (yas-global-mode 1)
       (setq yas-indent-line nil))
   #+END_SRC
** Org
   From [[https://github.com/hrs/dotfiles/tree/master/emacs.d][hrs]] config file but converted to use-package
 
   Use pretty bullet points instead of asterix

   Use a little downward-pointing arrow instead of the usual ellipsis
   (=...=) when folded.

   Use syntax highlighting in source blocks while editing.
   #+BEGIN_SRC emacs-lisp
     (use-package org
       :mode ("\\.org\\'" . org-mode)
       :config
       (use-package org-bullets)
       (add-hook 'org-mode-hook
                 (lambda ()
                   (org-bullets-mode t)))
       (setq org-ellipsis "⤵")
       (setq org-src-fontify-natively t)
       (setq org-src-window-setup 'current-window))
   #+END_SRC

   Doesn't work with yasnippet getting:
   yas--fallback: yasnippet fallback loop!
   This can happen when you bind ‘yas-expand’ outside of the ‘yas-minor-mode-map’.

   Make TAB act as if it were issued in a buffer of the language's major mode.
   =(setq org-src-tab-acts-natively t)=
** Makefile
   Set following files to use makefile-gmake-mode as the default.
   - Files that starts with =Makefile=.
   - Has extension =.mk=.
   - Files that are located in a directory called Make and ends with
     Rules.
   - Files that are located in a directory called modules.
   - Files called BUILD.conf (pb2 file).
   #+BEGIN_SRC emacs-lisp
     (use-package make-mode
       :mode (("Makefile.*" . makefile-gmake-mode)
              ("\\.mk$" . makefile-gmake-mode)
              ("Make/.*Rules" . makefile-gmake-mode)
              ("modules/.*" . makefile-gmake-mode)
              ("BUILD.conf" . makefile-gmake-mode)
              ))
   #+END_SRC
** Evil numbers
   Incrementing/decrementing numbers.
   #+BEGIN_SRC emacs-lisp
     (use-package evil-numbers
     :bind (("C-c +" . evil-numbers/inc-at-pt)
            ("C-c -" . evil-numbers/dec-at-pt)))
   #+END_SRC
** Magit
   A Git porcelain inside Emacs
   Key =C-x g= to run magit on current buffer.

   #+BEGIN_SRC emacs-lisp
     (use-package magit
       :bind ( "C-x g" . magit-status))
   #+END_SRC

** Buffer move
   Move buffers around between windows
   #+BEGIN_SRC emacs-lisp
     (use-package buffer-move 
       :bind ( ("<M-S-up>"    . buf-move-up)
               ("<M-S-down>"  . buf-move-down)
               ("<M-S-left>"  . buf-move-left)
               ("<M-S-right>" . buf-move-right)))
   #+END_SRC
** Multiple cursors
   Keybindings for the mc package
   #+BEGIN_SRC emacs-lisp
     (use-package multiple-cursors 
       :bind (("C-S-c C-S-c" . mc/edit-lines)
              ("C->"         . mc/mark-next-like-this)
              ("C-<"         . mc/mark-previous-like-this)
              ("C-c C-<"     . mc/mark-all-like-this)
              ("C-+"         . mc/mark-next-like-this))
       :config
       (use-package mc-extras))
   #+END_SRC
** String inflections
   Keybinding for cycle between snake case, camel case etc
   #+BEGIN_SRC emacs-lisp
     (use-package string-inflection 
       :bind ("C-;" . string-inflection-cycle ))
   #+END_SRC
** Sudo edit
   Sudo edit the current file
   #+BEGIN_SRC emacs-lisp
     (use-package sudo-edit
       :bind ("C-c C-r" . sudo-edit))
   #+END_SRC
** Expand region
   #+BEGIN_SRC emacs-lisp
     (use-package expand-region
       :bind ("C-=" . er/expand-region))
   #+END_SRC

** GTags (Disabled)
   Key bindings for finding tag, reference and usage of symbol.

   Cycling gtag results ([[https://www.emacswiki.org/emacs/CyclingGTagsResult][source]])
   #+BEGIN_SRC emacs-lisp
     (use-package ggtags
       :disabled t
       :bind (("M-." . gtags-find-tag) ;; Finds tag
              ("C-M-." . gtags-find-rtag)   ;; Find all references of tag
              ("C-M-," . gtags-find-symbol)) ;; Find all usages of symbol.
       :config 
       (defun ww-next-gtag ()
             "Find next matching tag, for GTAGS."
             (interactive)
             (let ((latest-gtags-buffer
                    (car (delq nil  (mapcar (lambda (x) (and (string-match "GTAGS SELECT" (buffer-name x)) (buffer-name x)) )
                                            (buffer-list)) ))))
               (cond (latest-gtags-buffer
                      (switch-to-buffer latest-gtags-buffer)
                      (forward-line)
                      (gtags-select-it nil))
                     ))))

   #+END_SRC
   
** Windmove
   Jump between windows using the arrow keys instead of cycling with
   "C-x o". Note that this Doesn't work in org mode.
   #+BEGIN_SRC emacs-lisp
     (use-package windmove 
       :bind (([M-left]  . windmove-left)   ; move to left window
              ([M-right] . windmove-right) ; move to right window
              ([M-up]    . windmove-up)       ; move to upper window
              ([M-down]  . windmove-down)))   ; move to downer window
   #+END_SRC

** Move text
   Move line up and down using arrow keys.
   #+BEGIN_SRC emacs-lisp
     (use-package move-text
       :bind (([C-S-up] . move-text-up)
              ([C-S-down] . move-text-down)))
   #+END_SRC
** The Silver Searcher
   #+BEGIN_SRC emacs-lisp
     (use-package ag)
   #+END_SRC
   
** ibuffer
    Use ibuffer instead of list-buffers, has some neat features.

    Sort buffers by placing them in different groups
    #+BEGIN_SRC emacs-lisp
      (use-package ibuffer
        :config 
        (defalias 'list-buffers 'ibuffer)
        (setq ibuffer-saved-filter-groups
            (quote (("default"
                     ("c++" (mode . c++-mode))
                     ("dired" (mode . dired-mode))
                     ("ag" (mode . ag-mode ))
                     ("emacs" (or
                               (name . "^\\*scratch\\*$")
                               (name . "^\\*Messages\\*$")
                               (name . "^\\*Completions\\*$")))
                     ))))
      (add-hook 'ibuffer-mode-hook
                (lambda ()
                  (ibuffer-switch-to-saved-filter-groups "default"))))
    #+END_SRC

** Multi term
   #+BEGIN_SRC emacs-lisp
     (use-package multi-term)
   #+END_SRC
** Rainbow
   #+BEGIN_SRC emacs-lisp
     (use-package rainbow-mode)   
   #+END_SRC
* Work
** Custom packages
*** dd-newfile
    Function that inserts the DD template for a new file
    #+BEGIN_SRC emacs-lisp
      (use-package dd-newfile
        :if dotemacs-is-work
        :load-path "~/.emacs.d/plugins")
    #+END_SRC
*** dd-log-parser
    Functions for parsing the =DD::Logger=
    #+BEGIN_SRC emacs-lisp
      (use-package dd-log-parser
        :if dotemacs-is-work
        :load-path "~/.emacs.d/plugins")
    #+END_SRC
*** dd-pybuild2
    #+BEGIN_SRC emacs-lisp
      (use-package dd-pybuild2
       	:if dotemacs-is-work
       	:load-path "~/.emacs.d/plugins")
    #+END_SRC
*** houdini
    Houdini related functions, mostly handle houdini versions.
    #+BEGIN_SRC emacs-lisp
            (use-package houdini
              :bind ("C-x j" . hou-insert-version)
              :if dotemacs-is-work
              :load-path "~/.emacs.d/plugins")
    #+END_SRC
*** highlight-extra
    Functions for highlighting my shells when building etc
    #+BEGIN_SRC emacs-lisp
      (use-package highlight-extra
        :if dotemacs-is-work
        :load-path "~/.emacs.d/plugins")
    #+END_SRC
*** work
    Bunch of functions to setup my work area when at work 
    #+BEGIN_SRC emacs-lisp
      (use-package work
        :if dotemacs-is-work
        :load-path "~/.emacs.d/plugins")
    #+END_SRC
** Custom functions
*** PID    
   Get the pid of a proc
   #+BEGIN_SRC emacs-lisp
     (defun pid (regex &optional index)
       "Get the pid of REGEX, if more than one is running it returns one
     at INDEX. Where INDEX starts from 0 and up"
       (interactive)
       (when (not index) (setq index 0))
       (nth index 
	    (split-string 
	     (shell-command-to-string
	      (concat "ps aux | " ;; wrap first character in [ ] to not match itself
		      "sed -nE \"s/$USER\\s+([0-9]+).*?"
		      (concat "[" (substring regex 0 1) "]" (substring regex 1))"/\\1/p\"")
	      ))))
   #+END_SRC
*** PID Houdini
    Get the PID for houdini
    #+BEGIN_SRC emacs-lisp
      (defun pid-houdini (&optional index )
	"Get the pid for houdini.
      If more than one is running it returns the one at INDEX.  Where
      INDEX starts from 0 and up"

	(interactive)
	(when (not index) (setq index 0)) (pid "houdini-bin" index))
    #+END_SRC
*** PID Maya
    #+BEGIN_SRC emacs-lisp
      (defun pid-maya (&optional index)
	"Get the pid for maya.
      If more than one is running it returns the one at INDEX.  Where
      INDEX starts from 0 and up"
	(interactive)
	(when (not index) (setq index 0))
	(pid "maya\\.bin" index))
    #+END_SRC
*** PID smeat
    #+BEGIN_SRC emacs-lisp
      (defun pid-smeat (&optional index)
	"Get the pid for smeat.
      If more than one is running it returns the one at INDEX.  Where
      INDEX starts from 0 and up"
	(interactive)
	(when (not index) (setq index 0))
	;; the ^= is to ignore houdini/python commands e.g houdini --with smeat=...
	(pid "smeat(:?[^=]+|$$)" index))
    #+END_SRC
*** Attach Houdini
    Used with gdb, prints attach <pid of houdini> in the prompt.
    #+BEGIN_SRC emacs-lisp
      (defun attach-houdini (&optional index)
	"Prints attach <pid> into the buffer. 
      INDEX is use to select which one if there are multiple instances
      running, INDEX counts from 1."
	(interactive"p")

	;; The default for index is one.
	(when (< index 1) (setq index 1))
	(insert (concat "attach " (pid-houdini (- index 1)) )))
    #+END_SRC
*** Attach Maya
    #+BEGIN_SRC emacs-lisp
      (defun attach-maya (&optional index) 
        "Prints attach <pid> into the buffer. 
      INDEX is use to select which one if there are multiple instances
      running, INDEX counts from 1."
        (interactive"p")
        ;; The default for index is one.
        (when (< index 1) (setq index 1))
        (insert (concat "attach " (pid-maya index) )))
    #+END_SRC
*** Attach smeat
    #+BEGIN_SRC emacs-lisp
      (defun attach-smeat (&optional index)
	"Prints attach <pid> into the buffer.
      INDEX is use to select which one if there are multiple instances
      running, INDEX counts from 1."
	(interactive"p")

	;; The default for index is one.
	(when (< index 1) (setq index 1))
	(insert (concat "attach " (pid-smeat (- index 1)) )))
    #+END_SRC
*** Kill Houdini
    #+BEGIN_SRC emacs-lisp
      (defun kill-houdini ()
        "Kill houdini.
      If more than one houdini are running it will kill the
      first one in the ps list."
        (interactive)
        (shell-command (concat "kill -9 " (pid-houdini))))
    #+END_SRC
*** Kill Maya 
    #+BEGIN_SRC emacs-lisp
      (defun kill-maya ()
        "Kill maya.
      If more than one Maya process are running it will kill the
      first one in the ps list."
        (interactive)
        (shell-command (concat "kill -9 " (pid-maya))))
    #+END_SRC
*** Smeat abort
    Not quite working. But sends a signal to the smeat process to abort the sim.
    #+BEGIN_SRC emacs-lisp
      (defun smeat-abort (&optional index )
        "Sends USR1 signal to houdini which aborts the smeat client.
      INDEX is used to select which houdini instance to send to if
      multiple instances exist."
        (interactive"p")
        (when (< index 1) (setq index 1))
        (let ((hou-pid (pid-houdini index)))
          (shell-command (concat "kill -s USR1 " hou-pid))))
    #+END_SRC
*** Preproccess
    #+BEGIN_SRC emacs-lisp
      (defun preprocess-fix-macros ()
        "Fix expanded macros when running only the preprocess on a file.
      For example: g++ <flags> -E <file>.  Since they are expanded into
      a single line which makes them hard to debug."
        (interactive)
        (let* ((start (if (use-region-p) (region-beginning) (point)))
               (end (if (use-region-p) (region-end) (point-max)))
               (regex-map '(":[ ]" ";" "{" "}[ ]"))
               (regex (mapconcat (lambda (x) (format "\\(%s\\)" x)) regex-map "\\|")))
          (goto-char start)
          (while (search-forward-regexp regex end t)
            (newline)
            (setq end (1+ end)))
          (indent-region start (point))
          (goto-char start)))
    #+END_SRC
* Custom functions
** Buffer
   Function for renaming buffer and file. Source: [[http://www.stringify.com/2006/apr/24/rename/][Link]]
   #+BEGIN_SRC emacs-lisp
     (defun rename-current-file-or-buffer ()
       "Rename current file and buffer, similar to save-as but removes
     the old file"
       (interactive)
       (if (not (buffer-file-name))
           (call-interactively 'rename-buffer)
         (let ((file (buffer-file-name)))
           (with-temp-buffer
             (set-buffer (dired-noselect file))
             (dired-do-rename)
             (kill-buffer nil))))
       nil)
   #+END_SRC
   To sync all open buffers with their respective files on disk. Source: [[https://www.emacswiki.org/emacs/RevertBuffer][here]]
   #+BEGIN_SRC emacs-lisp
     (defun revert-all-buffers ()
         "Refreshes all open buffers from their respective files."
         (interactive)
         (dolist (buf (buffer-list))
           (with-current-buffer buf
             (when (and (buffer-file-name) (not (buffer-modified-p)))
               (revert-buffer t t t) )))
         (message "Refreshed open files.") )
   #+END_SRC
** Text
   Taken from [[http://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs][here]], author mk-fg.
   #+BEGIN_SRC emacs-lisp
     (defun duplicate-line ()
       "Clone line at cursor, leaving the latter intact."
       (interactive)
       (save-excursion
         (let ((kill-read-only-ok t) deactivate-mark)
           (read-only-mode 1)
           (kill-whole-line)
           (read-only-mode 0)
           (yank))))
   #+END_SRC
** Programming
*** C++
    Insert java style comment
    /**
     * <cursor>
     */
    #+BEGIN_SRC emacs-lisp
      (defun insert-function-comment ()
        (interactive)
        (insert "/**")  (indent-according-to-mode)
        (insert "\n* ") (indent-according-to-mode)
        (insert "\n*/") (indent-according-to-mode)
        (forward-line -1)
        (end-of-line))
    #+END_SRC

    Expands a define macro for all matches in current buffer.
    #+BEGIN_SRC emacs-lisp
      (defun replace-define()
        "Evaluating the define variable.
      Place cursor on a #define <var> <content> and execute this command and it will
      replace all <var> with <content> in the file."
        (interactive)
        (let ((line (split-string (thing-at-point 'line) )))
          (if (equal (car line) "#define")
              (let ((curr-pos (point)) ;; save current position
                    (end (point-max)))
                ;; Jump to the end of line
                (end-of-line)
                ;; Replace the first with the second.
                (while (re-search-forward (concat "\\_<"(nth 1 line)"\\_>") end t )
                  (replace-match (nth 2 line)))
                ;; return to the same position
                (goto-char curr-pos)
                ;; move to the end of the line to indicate that it's done.
                (end-of-line))
            (message "Not a #define directive!" ))))
    #+END_SRC

    Undo the previous function.
    #+BEGIN_SRC emacs-lisp
      (defun replace-define-undo()
        "Undoing the expansion of the define variable.
      Place cursor on a #define <var> <content> and execute this
       command and it will replace all <content> with <var> in the
       file."

        (interactive)
        (let ((line (split-string (thing-at-point 'line) )))
          (if (equal (car line) "#define")
              (let ((curr-pos (point)) ;; save current position
                    (end (point-max)))
                ;; Jump to the end of line
                (end-of-line)

                ;; Replace the second with the first
                (while (re-search-forward (nth 2 line) end t ) (replace-match (nth 1 line)))

                ;; return to the same position
                (goto-char curr-pos)
                ;; move to the end of the line to indicate that it's done.
                (end-of-line))
            (message "Not a #define directive!" ))))
    #+END_SRC

    Convert typedef to c++11's alias
    #+BEGIN_SRC emacs-lisp
      (defun convert-typedef-to-using ()
        "Converts typedef statements to using statements"
        (interactive)
        (let ((begin) (end))
          
          (if (use-region-p)
              (progn (setq begin (region-beginning) end (region-end)))
            (progn (setq begin (point) end nil)))
          
          (goto-char begin)
          (while (re-search-forward
                  (concat "typedef \\(\\(?:typename \\)*"
                          "[a-zA-Z0-9_:<>,*& ]+?\\)[ ]+\\([a-zA-Z0-9]+\\)[ ]*;" )
                  end t )
            (replace-match "using \\2 = \\1;"))))
    #+END_SRC
** Workspace
   Function for splitting emacs into three frames. 
   Really nice to use with i3wm.
   #+BEGIN_SRC emacs-lisp
     (defun setup-home ()
     "Splits the session into three frames"
     (interactive)
     (delete-other-frames)
     (delete-other-windows)
     (make-frame-command)
     (make-frame-command)
     )
   #+END_SRC
** Split lines
   Function for splitting lines at specified character. Default is ','.
   #+BEGIN_SRC emacs-lisp
     (defun split-at (&optional delim)
     "Split region/line at DELIM, if there are multiple matches it
     will split each one. DELIM will default to \",\" if no delim is
     given."
     (interactive "sSpecify delimiter: ")
     (when (or (string= delim "") (not delim)) (setq delim ","))
     (let ((start (if (use-region-p) (region-beginning) (point-at-bol)))
           (end (if (use-region-p) (region-end) (point-at-eol)))
           (regex delim))
       (goto-char start)
      
       (while (search-forward-regexp regex end t)
         (insert "\n")
         (setq end (1+ end))
         )
       (indent-region start end)
       (goto-char start)
       )
     )

     (defun split-at-comma ()
     "wrapper for split-at for use with key command"
     (interactive)
     (split-at ",")
     )
   #+END_SRC
** Yesterday-time
   Computes the time 24 hours ago
   #+BEGIN_SRC emacs-lisp
     (defun yesterday-time ()
     "Provide the date/time 24 hours before the time now in the format of current-time."
       (let* ((now-time (current-time))              ; get the time now
              (hi (car now-time))                    ; save off the high word
              (lo (car (cdr now-time)))              ; save off the low word
              (msecs (nth 2 now-time))               ; save off the milliseconds
              )

         (if (< lo 20864)                      ; if the low word is too small for subtracting
             (setq hi (- hi 2)  lo (+ lo 44672)) ; take 2 from the high word and add to the low
           (setq hi (- hi 1) lo (- lo 20864))  ; else, add 86400 seconds (in two parts)
           )
         (list hi lo msecs)                    ; regurgitate the new values
         ))
   #+END_SRC

* Custom plugins
** Multiple cursor extension
   #+BEGIN_SRC emacs-lisp
     (use-package mc-extra-extra
       :load-path "~/emacs.d/plugins")
   #+END_SRC
* Custom keybindings
** Macros
    Macro to quickly open a file that is located on my machine at work.
    Inserts "fredriks@bcws649.d2vancouver.com:fredriks/swdevl/CoreLibs"
    Old machine was bcbellws108.
    #+BEGIN_SRC emacs-lisp
      (fset 'bcws
         [?f ?r ?e ?d ?r ?i ?k ?s ?@ ?b ?c ?w ?s ?6 ?4 ?9 ?. ?d ?2 ?v ?a ?n ?c ?o ?u ?v ?e ?r ?. ?c ?o ?m ?: ?f ?r ?e ?d ?r ?i ?k ?s ?/ ?s ?w ?d ?e ?v ?l ?/ ?C ?o ?r ?e ?L ?i ?b ?s])
    #+END_SRC

    And bind it to the key combo
    #+BEGIN_SRC emacs-lisp
      (global-set-key (kbd "C-c B") 'bcws)
    #+END_SRC
** Registers
   Quickly jump to files by pressing C-x r j <register>
   Jump to my init file with 'e' and init directory with 'i'.
   #+BEGIN_SRC emacs-lisp
     (set-register ?e (cons 'file "~/.emacs.d/init.el")) 
     (set-register ?i (cons 'file "~/.emacs.d/init.d")) 
   #+END_SRC
** UI
   Key bindings if I really need to see the menu and tool bar
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "<f5>") 'menu-bar-mode)
     (global-set-key (kbd "<f6>") 'tool-bar-mode)
   #+END_SRC
   
** Navigation
   Jump to specific line.
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "M-g") 'goto-line)
   #+END_SRC
** Text search
   I'm using the regex variant of the text search more than the normal one.
   Swapping keybindings for them
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "C-M-s") 'isearch-forward)
     (global-set-key (kbd "C-M-r") 'isearch-backward)
     (global-set-key (kbd "C-s") 'isearch-forward-regexp)
     (global-set-key (kbd "C-r") 'isearch-backward-regexp)
     (global-set-key (kbd "C-S-s") 'isearch-forward-symbol-at-point)
   #+END_SRC
** Text edit
   Bind replace regexp to meta r
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "M-r") 'replace-regexp)
   #+END_SRC
   
   Duplicate line
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "C-c l") 'duplicate-line)
   #+END_SRC

   Split line at comma
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "C-,") 'split-at-comma)
   #+END_SRC
** Buffer functions
   Key bindings for revert-all-buffers and rename current buffer
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "C-c r") 'revert-all-buffers)
     (global-set-key "\C-cR" 'rename-current-file-or-buffer)
   #+END_SRC
** C Common
   Hide/Show code blocks
   #+BEGIN_SRC emacs-lisp
     (add-hook 'c-mode-common-hook
       (lambda()
         (local-set-key (kbd "C-c <right>") 'hs-show-block)
         (local-set-key (kbd "C-c <left>")  'hs-hide-block)
         (local-set-key (kbd "C-c <up>")    'hs-hide-all)
         (local-set-key (kbd "C-c <down>")  'hs-show-all)
         (hs-minor-mode t)))
   #+END_SRC

   When in a c family buffer use shift tab to switch between header
   and source
   #+BEGIN_SRC emacs-lisp
     (add-hook 'c-mode-common-hook
       (lambda() 
         (local-set-key  (kbd "<backtab>") 'ff-find-other-file)))
   #+END_SRC
** Compile
   Key bindings to run make on current location and to re-run the
   command.
   #+BEGIN_SRC emacs-lisp
     (global-set-key (kbd "<f12>") 'compile)
     (global-set-key (kbd "<f11>") 'recompile)
   #+END_SRC
* Bug workarounds
  Workarounds for bugs I have encountered through out the years
** Cursor turns black
   Set the cursor color to white.
   #+BEGIN_SRC emacs-lisp
     (set-cursor-color "#ffffff")
   #+END_SRC
** Maximize emacs under KDE
   Issue maximizing emacs with KDE at work
   #+BEGIN_SRC emacs-lisp
     (setq frame-resize-pixelwise t)
   #+END_SRC
* Notes
** Lisp in search replace
   To execute a lisp function in replace regexp do \,(<function>)
** Skip code block
   You can use =:tangle no= in the =SRC_BLOCK= to ignore the code
   block from being exported aka tangled. Good for example blocks that
   you don't want to end up in you config file.
** Flycheck
*** Tweak flycheck
   To set specific compiler and flags for a specific project you can
   use something like this in a .dir-locals.el file:
   #+BEGIN_SRC emacs-lisp :tangle no
     ;; File .dir-locals.el
     ( ( c++-mode 
         . ( (flycheck-c/c++-gcc-executable . "/tools/package/gcc/6.2.0/bin/g++" )
             (flycheck-gcc-args 
              . ("-I/dd/dept/software/users/fredriks/swdevl/PRIVATE/include"
                 "-isystem/dd/tools/cent6_64/package/blosc/1.5.0/include"
                 "-isystem/dd/tools/cent6_64/package/eigen/3.2.10/include/eigen3"
                 "-isystem/dd/tools/cent6_64/package/hdf5/1.8.8/include"
                 "-isystem/dd/tools/cent6_64/package/ilmbase/2.2.0/include"
                 "-isystem/dd/tools/cent6_64/package/mkl/11.2.3/include"
                 "-isystem/dd/tools/cent6_64/package/openvdb/3.2.0/include"
                 "-isystem/dd/tools/cent6_64/package/boost/1.55.0/include"
                 "-isystem/dd/tools/cent6_64/package/tbb/4.3.1/include"
                 "-isystem/dd/tools/cent6_64/package/openmesh/3.3.0/include"
                 "-isystem/tools/include"
                 "-std=c++0x"
                 "-Wall"
                 "-D_GLIBCXX_USE_CXX11_ABI=0"
                 "-DEIGEN_MATRIXBASE_PLUGIN=<DD/Utility/EigenMatrixBaseAddon.hpp>"
                 "-DDD_OPEN_MESH_POLYMESHT_EXTENSION=<DD/Math/Geometry/OpenMesh/OpenMesh_PolyMeshT_Extension.hpp>"
                 "-DDD_OPEN_MESH_TRIMESHT_EXTENSION=<DD/Math/Geometry/OpenMesh/OpenMesh_TriMeshT_Extension.hpp>"
                 "-DDD_CORELIBS_USE_OPENMESH"
                 "-DDD_CORELIBS_BUILD_LIBS"
                 "-DEIGEN_MATRIXBASE_PLUGIN=<DD/Utility/EigenMatrixBaseAddon.hpp>")))))
   #+END_SRC

   If you want to play it more safe you can use the flychecks other
   variables (see C-c ! ?). For example this also works, but all
   include paths are prefix with -I so you'll get a lot of noise from
   boost etc.
   #+BEGIN_SRC emacs-lisp :tangle no
     ;; File .dir-locals.el
     ( ( c++-mode 
         . ( (flycheck-gcc-include-path 
              . ("/dd/dept/software/users/fredriks/swdevl/PRIVATE/include"
                 "/dd/tools/cent6_64/package/blosc/1.5.0/include"
                 "/dd/tools/cent6_64/package/eigen/3.2.10/include/eigen3"
                 "/dd/tools/cent6_64/package/hdf5/1.8.8/include"
                 "/dd/tools/cent6_64/package/ilmbase/2.2.0/include"
                 "/dd/tools/cent6_64/package/mkl/11.2.3/include"
                 "/dd/tools/cent6_64/package/openvdb/3.2.0/include"
                 "/dd/tools/cent6_64/package/boost/1.55.0/include"
                 "/dd/tools/cent6_64/package/tbb/4.3.1/include"
                 "/dd/tools/cent6_64/package/openmesh/3.3.0/include"
                 "/tools/include"))
             (flycheck-gcc-definitions 
              . ("_GLIBCXX_USE_CXX11_ABI=0"
                 "EIGEN_MATRIXBASE_PLUGIN=<DD/Utility/EigenMatrixBaseAddon.hpp>"
                 "DD_OPEN_MESH_POLYMESHT_EXTENSION=<DD/Math/Geometry/OpenMesh/OpenMesh_PolyMeshT_Extension.hpp>"
                 "DD_OPEN_MESH_TRIMESHT_EXTENSION=<DD/Math/Geometry/OpenMesh/OpenMesh_TriMeshT_Extension.hpp>"
                 "DD_CORELIBS_USE_OPENMESH"
                 "DD_CORELIBS_BUILD_LIBS"
                 "EIGEN_MATRIXBASE_PLUGIN=<DD/Utility/EigenMatrixBaseAddon.hpp>")))))
   #+END_SRC
*** Language standard in c++
   Specify language standard in dir locals file: .dir-locals.el 
   For example
   ((c++-mode
     (flycheck-clang-language-standard . "c++14")
     (flycheck-gcc-language-standard . "c++14")))
